package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.assistant.common.vo.BasePageVO;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.AuthStatusEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.base.common.vo.dict.DictAreaVO;
import com.bifang.module.base.common.vo.dict.SelectVO;
import com.bifang.module.base.common.vo.operationPeriod.OperationPeriodQueryVO;
import com.bifang.module.base.core.helper.DictHelper;
import com.bifang.module.base.core.integration.DictIntegration;
import com.bifang.module.base.core.integration.OperationPeriodIntegration;
import com.bifang.module.system.common.enums.ArticleStatusEnum;
import com.bifang.module.system.core.dao.model.AppArticle;
import com.bifang.module.system.core.dao.service.AppArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.enums.EnrollStatusEnum;
import com.jwsoft.manager.common.enums.SchoolCategoryEnum;
import com.jwsoft.manager.common.enums.SchoolConfigDictEnum;
import com.jwsoft.manager.common.enums.SchoolDictEnum;
import com.jwsoft.manager.common.vo.cache.EduOperationCacheDetailVO;
import com.jwsoft.manager.common.vo.cache.EduOperationCacheQueryVO;
import com.jwsoft.manager.common.vo.cache.EduOperationCacheVO;
import com.jwsoft.manager.common.vo.eduSchool.EduSchoolVO;
import com.jwsoft.manager.common.vo.eduSchoolConfig.*;
import com.jwsoft.manager.core.dao.model.*;
import com.jwsoft.manager.core.dao.service.*;
import com.jwsoft.manager.core.integration.EduSchoolConfigIntegration;
import com.jwsoft.manager.core.integration.EduSystemConfigIntegration;
import com.jwsoft.manager.core.integration.OperationCacheIntegration;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 所属学校配置表业务实现类
 *
 * @author hhj
 * @since 2022-12-21
 */
@ApiService(funcCode = "eduSchoolConfig", title = "学校招生计划管理")
public class EduSchoolConfigIntegrationImpl implements EduSchoolConfigIntegration {

    @Autowired
    public EduSchoolConfigService eduSchoolConfigService;
    @Autowired
    private EduFormEnrollService eduFormService;
    @Autowired
    private AppArticleService appArticleService;
    @Autowired
    private EduEnrollmenBrochureService eduEnrollmenBrochureService;
    @Autowired
    public DictHelper dictHelper;

    @Autowired
    public OperationPeriodIntegration operationPeriodIntegration;

    @Autowired
    public EduSystemConfigIntegration eduSystemConfigIntegration;

    @Autowired
    public EduHelper eduHelper;

    @Autowired
    public EduSchoolVacancyService eduSchoolVacancyService;

    @Autowired
    public EduAreaService eduAreaService;

    @Autowired
    public EduSchoolService eduSchoolService;

    @Autowired
    private DictIntegration dictIntegration;
    @Override
    @OpApi(funcCode = "eduSchoolConfig0001", title = "幼儿园学校招生计划管理分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduSchoolConfigVO> getList(EduSchoolConfigQueryVO vo) {

        List<String> areaCodeList = new ArrayList<>();
        if (ObjectUtils.isEmpty(vo.getAreaCode())){
            List<SelectVO> selectVOList = dictIntegration.getDistrictByOrg(new DictAreaVO());
            areaCodeList = selectVOList.stream()
                    .map(SelectVO::getDictKey)
                    .collect(Collectors.toList());
            String sessionAreaCode = SessionUtil.getAreaCode();
            if (!areaCodeList.contains(sessionAreaCode)) {
                areaCodeList.add(sessionAreaCode);
            }
        }else {
            areaCodeList.add(vo.getAreaCode());
        }
        vo.setAreaCodeList(areaCodeList);
        QueryWrapper<EduSchoolConfig> schoolConfigQueryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(vo.getYear())) {
            vo.setYear(eduHelper.thisYear());
        }
        int year=eduHelper.thisYear();
        if (year == vo.getYear()) {
            EduOperationCacheQueryVO cacheQueryVO = Convert.convert(EduOperationCacheQueryVO.class,vo);
            cacheQueryVO.setClassify("1");
            EduOperationCacheVO eduOperationCacheVO = eduSystemConfigIntegration.getEduOperation(cacheQueryVO);
            if (eduOperationCacheVO.getOperation() == null) {
                throw new AppException(year + "年度招生计划参数不存在");
            }
            EduOperationCacheDetailVO detailVO = eduOperationCacheVO.getOperation().get("YRY003");
            Boolean checkFlag = detailVO.isFlag();
            if (!checkFlag && "1".equalsIgnoreCase(detailVO.getType())) {
                throw new AppException("本年度幼儿园招生方案时间尚未开始");
            }
        }
        String userOrgId=SessionUtil.getOrgId();
        if (StringUtils.isNotBlank(userOrgId)){
            EduSchoolVO eduSchoolVO=eduSchoolService.getDetail(userOrgId);
            if (eduSchoolVO!=null){
                vo.setSchoolId(userOrgId);
            }
        }
        schoolConfigQueryWrapper
                .lambda()
                .eq(EduSchoolConfig::getSchoolCategory, SchoolCategoryEnum.KINDERGARTEN.getType())
                .eq(
                        StringUtils.isNotBlank(vo.getStatus()),
                        EduSchoolConfig::getStatus,
                        vo.getStatus())
                .eq(
                        StringUtils.isNotBlank(vo.getConfigType()),
                        EduSchoolConfig::getConfigType,
                        vo.getConfigType())
                .eq(!ObjectUtils.isEmpty(vo.getYear()), EduSchoolConfig::getYear, vo.getYear())
                .in(
                        !CollectionUtils.isEmpty(vo.getAreaCodeList()),
                        EduSchoolConfig::getAreaCode,
                        areaCodeList)
                .eq(
                        StringUtils.isNotBlank(vo.getSchoolId()),
                        EduSchoolConfig::getSchoolId,
                        vo.getSchoolId())
                .eq(
                        !ObjectUtils.isEmpty(vo.getEnrolment()),
                        EduSchoolConfig::getEnrolment,
                        vo.getEnrolment())
                .eq(
                        !ObjectUtils.isEmpty(vo.getStartAge()),
                        EduSchoolConfig::getStartAge,
                        vo.getStartAge())
                .eq(
                        !ObjectUtils.isEmpty(vo.getEndAge()),
                        EduSchoolConfig::getEndAge,
                        vo.getEndAge())
                .eq(
                        !ObjectUtils.isEmpty(vo.getStartTime()),
                        EduSchoolConfig::getStartTime,
                        vo.getStartTime())
                .eq(
                        !ObjectUtils.isEmpty(vo.getEndTime()),
                        EduSchoolConfig::getEndTime,
                        vo.getEndTime())
                .orderByDesc(EduSchoolConfig::getYear)
                .orderByAsc(EduSchoolConfig::getConfigType);

        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        List<EduSchoolConfig> list = eduSchoolConfigService.list(schoolConfigQueryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduSchoolConfig> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduSchoolConfigVO> resultList = new ArrayList<>();
        long curTime = DateUtil.currentSeconds();
        for (EduSchoolConfig eduSchoolConfig : pageInfo.getList()) {
            EduSchoolConfigVO schoolConfigVO = Convert.convert(EduSchoolConfigVO.class, eduSchoolConfig);
            if (BoolEnum.FALSE.getType().equals(schoolConfigVO.getStatus())) {
                schoolConfigVO.setEnrollStatus(EnrollStatusEnum.no.getType());
            } else {
                long startT = schoolConfigVO.getStartTime().getTime() / 1000;
                long endT = schoolConfigVO.getEndTime().getTime() / 1000;
                if (curTime < startT) {
                    schoolConfigVO.setEnrollStatus(EnrollStatusEnum.wait.getType());
                } else if (curTime > endT) {
                    schoolConfigVO.setEnrollStatus(EnrollStatusEnum.finish.getType());
                } else {
                    schoolConfigVO.setEnrollStatus(EnrollStatusEnum.on.getType());
                }
            }
            resultList.add(schoolConfigVO);
        }

        dictHelper.valueToName(resultList, Arrays.asList(SchoolConfigDictEnum.values()));


        return PagerUtil.parsePagerVo(resultList,pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduSchoolConfig0002", title = "学校招生计划管理根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduSchoolConfigVO getById(EduSchoolConfigKeyVO vo) {
        EduSchoolConfig entity = eduSchoolConfigService.getById(vo.getSchoolConfigId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduSchoolConfigVO result = Convert.convert(EduSchoolConfigVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "eduSchoolConfig0003", title = "幼儿园学校招生计划管理保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduSchoolConfigVO vo) {
        if(StringUtils.isBlank(vo.getSchoolId())){
            vo.setSchoolId(SessionUtil.getOrgId());
        }
        if(StringUtils.isBlank(vo.getSchoolId())){
            throw new AppException("学校id不能为空");
        }
        EduSchoolVO eduSchoolVO=eduSchoolService.getDetail(vo.getSchoolId());
        if(eduSchoolVO==null){
            throw new AppException("学校信息不存在");
        }
        if(StringUtils.isBlank(vo.getAreaCode())){
            vo.setAreaCode(eduSchoolVO.getAreaCode());
        }
        vo.setSchoolCategory(eduSchoolVO.getSchoolCategory());
        vo.setSchoolNature(eduSchoolVO.getSchoolNature());
        Integer year=eduHelper.thisYear();
        if(vo.getYear()==null){
            vo.setYear(year);
        }
        if (vo.getManagerFlag() == null) {
            //判断是否管理员
            if (StringUtils.isBlank(SessionUtil.getOrgId())) {
                vo.setManagerFlag(true);
            } else {
                EduSchoolVO checkSchool = eduSchoolService.getDetail(SessionUtil.getOrgId());
                if (checkSchool == null) {
                    vo.setManagerFlag(true);
                } else {
                    vo.setManagerFlag(false);
                }
            }
        }
        if (!vo.getManagerFlag()) {
            QueryWrapper<EduEnrollmenBrochure> brochureQueryWrapper = new QueryWrapper<>();
            brochureQueryWrapper.lambda().select(EduEnrollmenBrochure::getArticleId).eq(EduEnrollmenBrochure::getSchoolId, vo.getSchoolId())
                    .eq(EduEnrollmenBrochure::getYear, year);
            EduEnrollmenBrochure eduEnrollmenBrochure = eduEnrollmenBrochureService.getOne(brochureQueryWrapper, false);
            if (eduEnrollmenBrochure != null) {
                AppArticle appArticle = appArticleService.getById(eduEnrollmenBrochure.getArticleId());
                if (appArticle != null) {
                    if (!ArticleStatusEnum.draft.getType().equalsIgnoreCase(appArticle.getArticleStatus()) && !AuthStatusEnum.NO.getType().equalsIgnoreCase(appArticle.getAuthStatus())) {
                        throw new AppException("该招生方案已提交审核，不能修改，请先联系基教科退回");
                    }
                }
            }
        }

        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "eduSchoolConfig0004", title = "幼儿园学校招生计划管理新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduSchoolConfig0004",
            operationName = "幼儿园学校招生计划管理新增",
            dataType = "schoolConfigId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduSchoolConfigVO add(EduSchoolConfigVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        saveSchoolConfigInfo(vo);

        return vo;
    }

    @Override
    @OpApi(funcCode = "eduSchoolConfig0005", title = "幼儿园学校招生计划管理修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduSchoolConfig0005",
            operationName = "幼儿园学校招生计划管理修改",
            dataType = "schoolConfigId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduSchoolConfigVO edit(EduSchoolConfigVO vo) {
        if (ObjectUtils.isEmpty(vo.getSchoolConfigId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduSchoolConfig oldEntity = eduSchoolConfigService.getById(vo.getSchoolConfigId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        saveSchoolConfigInfo(vo);
        return vo;
    }
    private void saveSchoolConfigInfo(EduSchoolConfigVO vo) {
        QueryWrapper<EduSchoolConfig> schoolConfigNameQueryWrapper = new QueryWrapper<>();
        schoolConfigNameQueryWrapper
                .lambda()
                .eq(EduSchoolConfig::getStatus, BoolEnum.TRUE.getType())
                .eq(EduSchoolConfig::getYear, vo.getYear())
                .eq(EduSchoolConfig::getConfigType, vo.getConfigType())
                .eq(EduSchoolConfig::getSchoolId, vo.getSchoolId())
                .ne(
                        StringUtils.isNotBlank(vo.getSchoolConfigId()),
                        EduSchoolConfig::getSchoolConfigId,
                        vo.getSchoolConfigId());
        long countName = eduSchoolConfigService.count(schoolConfigNameQueryWrapper);
        if (countName > 0) {
            throw new AppException("同一年度，同一学段重复");
        }
        if (!vo.getManagerFlag()) {
            QueryWrapper<EduEnrollmenBrochure> brochureQueryWrapper = new QueryWrapper<>();
            brochureQueryWrapper
                    .lambda()
                    .eq(EduEnrollmenBrochure::getSchoolId, vo.getSchoolId())
                    .eq(EduEnrollmenBrochure::getYear, vo.getYear());
            EduEnrollmenBrochure brochure =
                    eduEnrollmenBrochureService.getOne(brochureQueryWrapper, false);
            if (!ObjectUtils.isEmpty(brochure)) {
                AppArticle article = appArticleService.getById(brochure.getArticleId());
                if (!ObjectUtils.isEmpty(article)) {
                    article.setArticleStatus(ArticleStatusEnum.draft.getType());
                    appArticleService.updateById(article);
                }
                QueryWrapper<EduFormEnroll> formQueryWrapper = new QueryWrapper<>();
                formQueryWrapper
                        .lambda()
                        .eq(EduFormEnroll::getFormRefId, brochure.getEnrollmenBrochureId())
                        .eq(EduFormEnroll::getFormRefName, "edu_enrollmen_brochure");
                List<EduFormEnroll> formList = eduFormService.list(formQueryWrapper);
                if (formList.size() > 0) {
                    formList.forEach(
                            item -> {
                                item.setStatus(ArticleStatusEnum.draft.getType());
                            });
                    eduFormService.updateBatchById(formList);
                }
            }
        }
        if(StringUtils.isBlank(vo.getStatus())){
            vo.setStatus(BoolEnum.TRUE.getType());
        }
        if (vo.getSerialNumEnd() == null) {
            vo.setSerialNumEnd(0);
        }
        if (StringUtils.isBlank(vo.getFullFlag())) {
            vo.setFullFlag(BoolEnum.FALSE.getType());
        }
        if (StringUtils.isBlank(vo.getSchoolConfigId())) {
            //新增
            EduSchoolConfig config = Convert.convert(EduSchoolConfig.class, vo);
            config.setSchoolConfigId(IdUtil.simpleUUID());
            eduSchoolConfigService.save(config);
            vo.setSchoolConfigId(config.getSchoolConfigId());
        }else{
            //修改
            EduSchoolConfig old = eduSchoolConfigService.getById(vo.getSchoolConfigId());
            //BeanUtil.copyProperties(vo,old);
            old.setConfigType(vo.getConfigType());
            old.setEnrolment(vo.getEnrolment());
            old.setStartAge(vo.getStartAge());
            old.setEndAge(vo.getEndAge());
            old.setStatus(vo.getStatus());
            old.setClassNum(vo.getClassNum());
            old.setStartTime(vo.getStartTime());
            old.setEndTime(vo.getEndTime());
            old.setSerialNumEnd(vo.getSerialNumEnd());
            old.setFullFlag(vo.getFullFlag());
            eduSchoolConfigService.updateById(old);
        }
        eduSchoolService.clearCache(vo.getSchoolId(),vo.getAreaCode());

    }

    @Override
    @OpApi(funcCode = "eduSchoolConfig0006", title = "学校招生计划管理根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduSchoolConfig0006",
            operationName = "所属学校配置表删除",
            dataType = "schoolConfigId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduSchoolConfigKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getSchoolConfigId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduSchoolConfig entity = eduSchoolConfigService.getById(vo.getSchoolConfigId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        if (entity.getStartTime()!=null&&new Date().after(entity.getStartTime())){
            throw new AppException("报名已开始，不能删除!");
        }
        eduSchoolConfigService.removeById(vo.getSchoolConfigId());
    }


    @Override
    @OpApi(funcCode = "eduSchoolConfig0009", title = "学校招生计划管理复制上年学校配置", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduSchoolConfig0009",
            operationName = "复制上年学校配置",
            dataType = "schoolConfigId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void copyLastYearConfig(SchoolConfigSaveVO vo) {
        String schoolId = vo.getSchoolId();
        Assert.hasText(schoolId, "学校id不能为空！");
        int year = eduHelper.thisYear();

        EduSchoolVO eduSchoolVO = eduSchoolService.getDetail(schoolId);
        if (eduSchoolVO == null) {
            throw new AppException("学校信息不存在");
        }
        String area = eduSchoolVO.getAreaCode();
        if(StringUtils.isBlank(area)){
            throw new AppException("学校的统筹区不能为空");
        }

        Date startAge = null;
        Date endAge = null;
        Date startTime = null;
        Date endTime = null;
        boolean yeyFlag = false;
        EduOperationCacheVO eduOperationCacheVO = null;
        if (SchoolCategoryEnum.KINDERGARTEN.getType().equalsIgnoreCase(eduSchoolVO.getSchoolCategory())) {
            //幼儿园
            EduOperationCacheQueryVO queryVO = new EduOperationCacheQueryVO();
            queryVO.setClassify("1");
            queryVO.setAreaCode(eduSchoolVO.getAreaCode());
            queryVO.setYear(year);
            eduOperationCacheVO = eduSystemConfigIntegration.getEduOperation(queryVO);
            if (eduOperationCacheVO != null) {
                EduOperationCacheDetailVO detailVO = eduOperationCacheVO.getOperation().get("YRY001");
                startTime = detailVO.getBegTime();
                endTime = detailVO.getEndTime();
            }
            yeyFlag = true;
        } else {
            EduOperationCacheQueryVO queryVO = new EduOperationCacheQueryVO();
            queryVO.setClassify("1");
            queryVO.setAreaCode(eduSchoolVO.getAreaCode());
            queryVO.setYear(year);
            eduOperationCacheVO = eduSystemConfigIntegration.getEduOperation(queryVO);
            if (eduOperationCacheVO != null) {
                EduOperationCacheDetailVO bmVO = eduOperationCacheVO.getOperation().get("YW002");
                startTime = bmVO.getBegTime();
                endTime = bmVO.getEndTime();
                if (SchoolCategoryEnum.PRIMARY.getType().equalsIgnoreCase(eduSchoolVO.getSchoolCategory())) {
                    EduOperationCacheDetailVO detailVO = eduOperationCacheVO.getOperation().get("YW005");
                    startAge = detailVO.getBegTime();
                    endAge = detailVO.getEndTime();
                    //小学
                } else if (SchoolCategoryEnum.JUNIOR.getType().equalsIgnoreCase(eduSchoolVO.getSchoolCategory())) {
                    //初中
                    EduOperationCacheDetailVO detailVO = eduOperationCacheVO.getOperation().get("YW006");
                    startAge = detailVO.getBegTime();
                    endAge = detailVO.getEndTime();
                }
            }

        }
        int lastYear = year - 1;
        QueryWrapper<EduSchoolConfig> schoolConfigQueryWrapper = new QueryWrapper<>();
        schoolConfigQueryWrapper
                .lambda()
                .eq(EduSchoolConfig::getSchoolId, schoolId)
                .eq(EduSchoolConfig::getStatus, BoolEnum.TRUE.getType())
                .eq(EduSchoolConfig::getYear, lastYear);
        List<EduSchoolConfig> oldConfigList = eduSchoolConfigService.list(schoolConfigQueryWrapper);
        if (CollectionUtils.isEmpty(oldConfigList)) {
            throw new AppException("上年度没有数据，请新增");
        }
        for (int i = 0; i < oldConfigList.size(); i++) {
            QueryWrapper<EduSchoolConfig> checkQueryWrapper = new QueryWrapper<>();
            checkQueryWrapper
                    .lambda()
                    .eq(EduSchoolConfig::getConfigType, oldConfigList.get(i).getConfigType())
                    .eq(EduSchoolConfig::getStatus, BoolEnum.TRUE.getType())
                    .eq(EduSchoolConfig::getYear, year)
                    .eq(EduSchoolConfig::getSchoolId,schoolId);
            int count = eduSchoolConfigService.list(checkQueryWrapper).size();
            if (count > 0) {
                throw new AppException("当前学校已存在相同学段，请选择其他学段");
            }
            oldConfigList.get(i).setYear(year);
            oldConfigList.get(i).setSchoolConfigId(IdUtil.simpleUUID());
            oldConfigList.get(i).setAreaCode(area);
            oldConfigList.get(i).setAuthStatus(AuthStatusEnum.NO.getType());
            oldConfigList.get(i).setAuthRemarks("");
            oldConfigList.get(i).setAuthTime(null);
            oldConfigList.get(i).setAuthUserId(null);
            oldConfigList.get(i).setFullFlag(BoolEnum.FALSE.getType());
            oldConfigList.get(i).setSerialNumEnd(0);
            oldConfigList.get(i).setStartTime(startTime);
            oldConfigList.get(i).setEndTime(endTime);
            if (yeyFlag && eduOperationCacheVO != null) {
                if ("1".equalsIgnoreCase(oldConfigList.get(i).getConfigType())) {
                    //托班
                    EduOperationCacheDetailVO detailVO = eduOperationCacheVO.getOperation().get("YRY006");
                    startAge = detailVO.getBegTime();
                    endAge = detailVO.getEndTime();
                } else if ("2".equalsIgnoreCase(oldConfigList.get(i).getConfigType())) {
                    //小班
                    EduOperationCacheDetailVO detailVO = eduOperationCacheVO.getOperation().get("YRY007");
                    startAge = detailVO.getBegTime();
                    endAge = detailVO.getEndTime();
                } else if ("3".equalsIgnoreCase(oldConfigList.get(i).getConfigType())) {
                    //中班
                    EduOperationCacheDetailVO detailVO = eduOperationCacheVO.getOperation().get("YRY008");
                    startAge = detailVO.getBegTime();
                    endAge = detailVO.getEndTime();
                } else if ("4".equalsIgnoreCase(oldConfigList.get(i).getConfigType())) {
                    //大班
                    EduOperationCacheDetailVO detailVO = eduOperationCacheVO.getOperation().get("YRY009");
                    startAge = detailVO.getBegTime();
                    endAge = detailVO.getEndTime();
                } else if ("5".equalsIgnoreCase(oldConfigList.get(i).getConfigType())) {
                    //混龄班
                    EduOperationCacheDetailVO detailVO = eduOperationCacheVO.getOperation().get("YRY010");
                    startAge = detailVO.getBegTime();
                    endAge = detailVO.getEndTime();
                }
            } else {

                oldConfigList.get(i).setStartAge(startAge);
                oldConfigList.get(i).setEndAge(endAge);
            }

        }


        eduSchoolConfigService.saveBatch(oldConfigList);
        eduSchoolService.clearCache(eduSchoolVO.getOrgId(), eduSchoolVO.getAreaCode());
    }



    @Override
    @OpApi(funcCode = "eduSchoolConfig0011", title = "幼儿园停止招生", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduSchoolConfig0011",
            operationName = "幼儿园停止招生",
            dataType = "formDetailId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void stopNurseryRecruit(EduSchoolConfigKeyVO vo) {
        if (StringUtils.isBlank(vo.getSchoolConfigId())) {
            throw new AppException("招生计划id不能为空");
        }
        EduSchoolConfig eduSchoolConfig = eduSchoolConfigService.getById(vo.getSchoolConfigId());
        if (eduSchoolConfig == null) {
            throw new AppException("招生计划不存在");
        }
        EduOperationCacheQueryVO queryVO = new EduOperationCacheQueryVO();
        queryVO.setAreaCode(eduSchoolConfig.getAreaCode());
        queryVO.setClassify("1");
        queryVO.setOperating("YRY001");
        EduOperationCacheDetailVO eduOperationCacheDetailVO = eduSystemConfigIntegration.getEduOperationOne(queryVO);
        if (eduOperationCacheDetailVO == null) {
            throw new AppException("招生报名未开始不能停止招生，如需停止，请联系幼儿园管理员退回招生方案后修改状态");
        }
        if (DateUtil.compare(new Date(), eduOperationCacheDetailVO.getBegTime()) < 0) {
            throw new AppException("招生报名未开始不能停止招生，如需停止，请联系幼儿园管理员退回招生方案后修改状态");
        }
        eduSchoolConfig.setEndTime(new Date());
        eduSchoolConfigService.updateById(eduSchoolConfig);
        eduSchoolService.clearCache(eduSchoolConfig.getSchoolId(), eduSchoolConfig.getAreaCode());
    }




    @Override
    @OpApi(funcCode = "eduSchoolConfig0012", title = "幼儿园学校招生计划管理获取招生开始时间", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE)
    public SchoolConfigQueryVO getEnrollStartTime(BasePageVO vo) {
        String area=  SessionUtil.getSessionInfo().getAreaCode();
        EduOperationCacheQueryVO cacheQueryVO = new EduOperationCacheQueryVO();
        cacheQueryVO.setClassify("1");
        cacheQueryVO.setOperating("YRY001");
        cacheQueryVO.setAreaCode(area);
        EduOperationCacheDetailVO eduOperationCacheVO = eduSystemConfigIntegration.getEduOperationOne(cacheQueryVO);
        SchoolConfigQueryVO result = new SchoolConfigQueryVO();
        if(!ObjectUtils.isEmpty(eduOperationCacheVO)) {
            result.setStartTime(eduOperationCacheVO.getBegTime());
            result.setEndTime(eduOperationCacheVO.getEndTime());
        }
        return result;
    }


    @Override
    @OpApi(funcCode = "eduSchoolConfig0013", title = "幼儿园学校招生计划管理获取招生学段年龄时间", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE)
    public SchoolConfigQueryVO getAgeTime(SchoolConfigQueryVO vo) {
        String area=  SessionUtil.getSessionInfo().getAreaCode();
        String bm = "YRY006";
        if("1".equals(vo.getConfigType())){
            bm = "YRY006";
        }else if("2".equals(vo.getConfigType())){
            bm = "YRY007";
        }else if("3".equals(vo.getConfigType())){
            bm = "YRY008";
        }else if("4".equals(vo.getConfigType())){
            bm = "YRY009";
        }else if("5".equals(vo.getConfigType())){
            bm = "YRY010";
        }

        OperationPeriodQueryVO operationPeriodQueryVO =
                Convert.convert(OperationPeriodQueryVO.class, vo);
        operationPeriodQueryVO.setOperating(bm);
        operationPeriodQueryVO.setAreaCode(area);


        EduOperationCacheQueryVO cacheQueryVO = new EduOperationCacheQueryVO();
        cacheQueryVO.setClassify("1");
        cacheQueryVO.setOperating(bm);
        cacheQueryVO.setAreaCode(area);
        EduOperationCacheDetailVO eduOperationCacheVO = eduSystemConfigIntegration.getEduOperationOne(cacheQueryVO);
        SchoolConfigQueryVO result = new SchoolConfigQueryVO();
        if(!ObjectUtils.isEmpty(eduOperationCacheVO)) {
            result.setStartAge(eduOperationCacheVO.getBegTime());
            result.setEndAge(eduOperationCacheVO.getEndTime());
        }
        return result;
    }

    @Override
    @OpApi(funcCode = "eduSchoolConfig0014", title = "义务教育学校招生计划管理查询", funcType = FuncTypeEnum.query)
    public PageInfo<SchoolConfigVO> getYwSchoolConfigInfo(SchoolConfigQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(100);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<String> areaCodeList = new ArrayList<>();
        if (ObjectUtils.isEmpty(vo.getAreaCode())){
            List<SelectVO> selectVOList = dictIntegration.getDistrictByOrg(new DictAreaVO());
            areaCodeList = selectVOList.stream()
                    .map(SelectVO::getDictKey)
                    .collect(Collectors.toList());
            String sessionAreaCode = SessionUtil.getAreaCode();
            if (!areaCodeList.contains(sessionAreaCode)) {
                areaCodeList.add(sessionAreaCode);
            }
        }else {
            areaCodeList.add(vo.getAreaCode());
        }
        vo.setAreaCodeList(areaCodeList);
        List<SchoolConfigAllVO> list = eduSchoolConfigService.getSchoolConfigInfo(vo);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<SchoolConfigAllVO> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<SchoolConfigVO> resultList = new ArrayList<>();
        long curTime = DateUtil.currentSeconds();
        int year= eduHelper.thisYear();
        String enrollStatus =EnrollStatusEnum.wait.getType();
        String enrollStatusName=EnrollStatusEnum.wait.getName();
       // EduEnrollPlan eduEnrollPlan = enrollPlanIntegration.getByYear(year);
        if(StringUtils.isBlank(vo.getAreaCode())){
            vo.setAreaCode(SessionUtil.getSessionInfo().getAreaCode());
        }
        EduOperationCacheQueryVO cacheQueryVO = Convert.convert(EduOperationCacheQueryVO.class,vo);
        cacheQueryVO.setClassify("2");
        EduOperationCacheVO eduOperationCacheVO = eduSystemConfigIntegration.getEduOperation(cacheQueryVO);
        if (eduOperationCacheVO.getOperation() != null) {
            if (!"3".equals(eduOperationCacheVO.getOperation().get("YW002").getType())) {
                long applyStart = eduOperationCacheVO.getOperation().get("YW002").getBegTime().getTime() / 1000;
                long applyEnd = eduOperationCacheVO.getOperation().get("YW002").getEndTime().getTime() / 1000;
                if (curTime >= applyStart && curTime <= applyEnd) {
                    enrollStatus = EnrollStatusEnum.on.getType();
                    enrollStatusName = EnrollStatusEnum.on.getName();
                } else if (curTime > applyEnd) {
                    enrollStatus = EnrollStatusEnum.auth.getType();
                    enrollStatusName = EnrollStatusEnum.auth.getName();
                    //判断空额抢报
                    if (!"3".equals(eduOperationCacheVO.getOperation().get("YW010").getType())) {
                        long spaceStart = eduOperationCacheVO.getOperation().get("YW010").getBegTime().getTime() / 1000;
                        long spaceEnd = eduOperationCacheVO.getOperation().get("YW010").getEndTime().getTime() / 1000;
                        if (curTime >= spaceStart && curTime <= spaceEnd) {
                            enrollStatus = EnrollStatusEnum.onVacancy.getType();
                            enrollStatusName = EnrollStatusEnum.onVacancy.getName();
                        } else if (spaceEnd > applyEnd) {
                            enrollStatus = EnrollStatusEnum.finish.getType();
                            enrollStatusName = EnrollStatusEnum.finish.getName();
                        }
                    }

                }
            }
        }

        // 获取学校空额名单
        Map<String,Integer> vacancyMap=new HashMap<>();
        List<String> schoolIdList=pageInfo.getList().stream().map(SchoolConfigAllVO::getSchoolId).distinct().collect(Collectors.toList());
        QueryWrapper<EduSchoolVacancy> vacancyQueryWrapper=new QueryWrapper<>();
        vacancyQueryWrapper.lambda().select(EduSchoolVacancy::getSchoolId,EduSchoolVacancy::getVacancyNum)
                .in(EduSchoolVacancy::getSchoolId,schoolIdList)
                .eq(EduSchoolVacancy::getAuthStatus, AuthStatusEnum.YES.getType())
                .eq(EduSchoolVacancy::getYear,year);
        List<EduSchoolVacancy> vacancyList=eduSchoolVacancyService.list(vacancyQueryWrapper);

        if(!CollectionUtils.isEmpty(vacancyList)){
            vacancyMap=vacancyList.stream().collect(Collectors.toMap(EduSchoolVacancy::getSchoolId, EduSchoolVacancy::getVacancyNum,(k1,k2)->k1));
        }
        Map<String, EduArea> areaMap =
                eduAreaService.list().stream()
                        .collect(Collectors.toMap(EduArea::getAreaCode, a -> a, (k1, k2) -> k1));
        for (SchoolConfigAllVO eduSchoolConfig : pageInfo.getList()) {
            if (!ObjectUtils.isEmpty(areaMap.get(eduSchoolConfig.getStreet()))) {
                eduSchoolConfig.setStreetName(areaMap.get(eduSchoolConfig.getStreet()).getAreaName());
            }
            SchoolConfigVO schoolConfigVO = Convert.convert(SchoolConfigVO.class, eduSchoolConfig);
            if (BoolEnum.FALSE.getType().equals(schoolConfigVO.getStatus())) {
                schoolConfigVO.setEnrollStatus(EnrollStatusEnum.no.getType());
                schoolConfigVO.setEnrollStatusName(EnrollStatusEnum.no.getName());
            } else {
                //状态统一使用参数配置的
                if (schoolConfigVO.getYear() > year) {
                    schoolConfigVO.setEnrollStatus(EnrollStatusEnum.wait.getType());
                    schoolConfigVO.setEnrollStatusName(EnrollStatusEnum.wait.getName());
                } else if (schoolConfigVO.getYear() < year) {
                    schoolConfigVO.setEnrollStatus(EnrollStatusEnum.finish.getType());
                    schoolConfigVO.setEnrollStatusName(EnrollStatusEnum.finish.getName());
                } else {
                    if (EnrollStatusEnum.onVacancy.getType().equals(enrollStatus)) {
                        Integer num = vacancyMap.get(schoolConfigVO.getSchoolId());
                        if (num == null) {
                            schoolConfigVO.setEnrollStatus(EnrollStatusEnum.finish.getType());
                            schoolConfigVO.setEnrollStatusName(EnrollStatusEnum.finish.getName());
                        } else {
                            if (num > 0) {
                                schoolConfigVO.setEnrollStatus(enrollStatus);
                                schoolConfigVO.setEnrollStatusName(enrollStatusName);
                            } else {
                                schoolConfigVO.setEnrollStatus(EnrollStatusEnum.finish.getType());
                                schoolConfigVO.setEnrollStatusName(EnrollStatusEnum.finish.getName());
                            }
                        }
                    } else {
                        schoolConfigVO.setEnrollStatus(enrollStatus);
                        schoolConfigVO.setEnrollStatusName(enrollStatusName);
                    }
                }
            }
            resultList.add(schoolConfigVO);
        }

        dictHelper.valueToName(resultList, Arrays.asList(SchoolDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduSchoolConfig0015", title = "义务教育学校招生计划管理新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduSchoolConfig0015",
            operationName = "义务教育学校配置表新增",
            dataType = "schoolConfigId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void saveYwSchoolConfigInfo(SchoolConfigSaveVO vo) {
        if (ObjectUtils.isEmpty(vo.getEnrolment())){
            throw new AppException("招生人数不能为空！");
        }
        if (ObjectUtils.isEmpty(vo.getClassNum())){
            throw new AppException("班级数不能为空！");
        }
        int year = eduHelper.thisYear();
        if (!ObjectUtils.isEmpty(vo.getYear()) && vo.getYear() != year) {
            throw new AppException("不是当前年度数据不允许处理");
        }

        if (StringUtils.isBlank(vo.getSchoolId())) {
            vo.setSchoolId(SessionUtil.getSessionInfo().getOrgId());
        }

        QueryWrapper<EduSchool> schoolQueryWrapper=new QueryWrapper<>();
        schoolQueryWrapper.lambda().eq(EduSchool::getOrgId,vo.getSchoolId())
                .eq(EduSchool::getStatus,BoolEnum.TRUE.getType())
                .eq(EduSchool::getRecruitStatus,BoolEnum.FALSE.getType());
        EduSchool eduSchool=eduSchoolService.getOne(schoolQueryWrapper,false);
        if(eduSchool==null){
            throw new AppException("学校不存在");
        }
        if(StringUtils.isBlank(vo.getAreaCode())){
            vo.setAreaCode(eduSchool.getAreaCode());
        }
        if(StringUtils.isBlank(vo.getAreaCode())){
            throw new AppException("行政区划不能为空");
        }
        EduSchoolConfig config = Convert.convert(EduSchoolConfig.class, vo);
        config.setSchoolCategory(eduSchool.getSchoolCategory());
        config.setSchoolNature(eduSchool.getSchoolNature());
        QueryWrapper<EduSchoolConfig> schoolConfigNameQueryWrapper = new QueryWrapper<>();
        schoolConfigNameQueryWrapper
                .lambda()
                .eq(EduSchoolConfig::getYear, year)
                .eq(EduSchoolConfig::getConfigType, vo.getConfigType())
                .eq(EduSchoolConfig::getSchoolId, vo.getSchoolId())
                .ne(
                        StringUtils.isNotBlank(config.getSchoolConfigId()),
                        EduSchoolConfig::getSchoolConfigId,
                        config.getSchoolConfigId());
        long countName = eduSchoolConfigService.count(schoolConfigNameQueryWrapper);
        if (countName > 0) {
            throw new AppException("同一年度，同一类别重复");
        }
        if (config.getOrderNo() == null) {
            config.setOrderNo(Integer.parseInt(vo.getConfigType()));
        }
        QueryWrapper<EduSchoolConfig> orderNoQueryWrapper = new QueryWrapper<>();
        orderNoQueryWrapper
                .lambda()
                .eq(EduSchoolConfig::getYear, year)
                .eq(EduSchoolConfig::getOrderNo, config.getOrderNo())
                .eq(EduSchoolConfig::getSchoolId, vo.getSchoolId())
                .ne(
                        StringUtils.isNotBlank(config.getSchoolConfigId()),
                        EduSchoolConfig::getSchoolConfigId,
                        config.getSchoolConfigId());
        long countOrderNo = eduSchoolConfigService.count(orderNoQueryWrapper);
        if (countOrderNo > 0) {
            throw new AppException("同一年度，同一排序号重复");
        }
        if (StringUtils.isBlank(config.getSchoolConfigId())) {
            config.setSchoolConfigId(IdUtil.simpleUUID());
            config.setYear(year);
            if("0".equals(eduSchool.getSchoolNature())){
                //公办学校，只能配置一个
                QueryWrapper<EduSchoolConfig> countW = new QueryWrapper<>();
                countW.lambda()
                        .eq(EduSchoolConfig::getYear, year)
                        .eq(EduSchoolConfig::getSchoolId, vo.getSchoolId())
                        .eq(EduSchoolConfig::getStatus,BoolEnum.TRUE.getType());
                if( eduSchoolConfigService.count(countW)>0){
                    throw new AppException("公办学校只能配置一个类型的招生计划");
                }
            }
        }else{
            EduSchoolConfig oldSchoolConfig = eduSchoolConfigService.getById(config.getSchoolConfigId());
            if (AuthStatusEnum.NODO.getType().equals(oldSchoolConfig.getAuthStatus())){
                throw new AppException("审核中数据不允许修改");
            }
        }
       // EduEnrollPlan eduEnrollPlan = enrollPlanIntegration.getByYear(year);
        EduOperationCacheQueryVO cacheQueryVO = Convert.convert(EduOperationCacheQueryVO.class,vo);
        cacheQueryVO.setClassify("2");
        EduOperationCacheVO eduOperationCacheVO = eduSystemConfigIntegration.getEduOperation(cacheQueryVO);
        if(eduOperationCacheVO.getOperation()==null){
            throw new AppException("招生计划参数未配置，请联系系统管理员");
        }
        if (vo.getSerialNumEnd() == null) {
            vo.setSerialNumEnd(0);
        }
        if (StringUtils.isBlank(vo.getFullFlag())) {
            vo.setFullFlag(BoolEnum.FALSE.getType());
        }
        config.setSchoolId(vo.getSchoolId());
        config.setStatus(vo.getStatus());
        if(vo.getAuditFlag()==null||!vo.getAuditFlag()){
            config.setAuthStatus(null);
            config.setAuthTime(null);
            config.setAuthRemarks(null);
            config.setAuthUserId(null);
        }

        config.setStatus(BoolEnum.TRUE.getType());
        if (SchoolCategoryEnum.PRIMARY.getType().equalsIgnoreCase(config.getSchoolCategory())) {
            //小学
            if (eduOperationCacheVO.getOperation().get("YW005") == null) {
                throw new AppException("招生计划参数，小学允许就读出生日期未配置");
            }

            if (ObjectUtils.isEmpty(eduOperationCacheVO.getOperation().get("YW005").getBegTime()) || ObjectUtils.isEmpty(eduOperationCacheVO.getOperation().get("YW005").getEndTime())){
                throw new AppException("招生计划参数“YW005” 未配置，请联系 "+ dictHelper.getAreaNameByCode(eduSchool.getAreaCode()) +" 管理员设置小学允许就读出生日期");
            }
            config.setStartAge(eduOperationCacheVO.getOperation().get("YW005").getBegTime());
            config.setEndAge(eduOperationCacheVO.getOperation().get("YW005").getEndTime());
        } else if (SchoolCategoryEnum.JUNIOR.getType().equalsIgnoreCase(config.getSchoolCategory())) {
            if (eduOperationCacheVO.getOperation().get("YW006") == null) {
                throw new AppException("招生计划参数，初中允许就读出生日期未配置");
            }
            if (ObjectUtils.isEmpty(eduOperationCacheVO.getOperation().get("YW006").getBegTime()) || ObjectUtils.isEmpty(eduOperationCacheVO.getOperation().get("YW005").getEndTime())){
                throw new AppException("招生计划参数“YW006” 未配置，请联系 "+ dictHelper.getAreaNameByCode(eduSchool.getAreaCode()) +" 管理员设置初中允许就读出生日期");
            }
            config.setStartAge(eduOperationCacheVO.getOperation().get("YW006").getBegTime());
            config.setEndAge(eduOperationCacheVO.getOperation().get("YW006").getEndTime());
        }
        if (eduOperationCacheVO.getOperation().get("YW002") == null) {
            throw new AppException("招生计划参数，家长报名业务期未配置");
        }
        if (ObjectUtils.isEmpty(eduOperationCacheVO.getOperation().get("YW002").getBegTime()) || ObjectUtils.isEmpty(eduOperationCacheVO.getOperation().get("YW005").getEndTime())){
            throw new AppException("招生计划参数“YW002” 未配置，请联系 "+dictHelper.getAreaNameByCode(eduSchool.getAreaCode())+" 管理员设置家长报名业务期");
        }
        config.setStartTime(eduOperationCacheVO.getOperation().get("YW002").getBegTime());
        config.setEndTime(DateUtil.parse(year + "0831"));
        config.setSerialNumEnd(vo.getSerialNumEnd());
        config.setFullFlag(vo.getFullFlag());
        eduSchoolConfigService.saveOrUpdate(config);
        eduSchoolService.clearCache(eduSchool.getOrgId(),eduSchool.getAreaCode());
    }


    @Override
    @OpApi(funcCode = "eduSchoolConfig0016", title = "义务教育学校招生计划管理更新", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduSchoolConfig0016",
            operationName = "义务教育学校招生计划管理更新",
            dataType = "schoolConfigId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void updateYwSchoolConfigInfo(SchoolConfigSaveVO vo) {
        if (StringUtils.isBlank(vo.getSchoolConfigId())) {
            throw new AppException("修改的id不能为空");
        }
        EduSchoolConfig old=eduSchoolConfigService.getById(vo.getSchoolConfigId());
        if(old==null){
            throw new AppException("修改的数据不存在");
        }
        if(AuthStatusEnum.YES.getType().equals(old.getAuthStatus())){
            throw new AppException("审核通过数据不允许修改");
        }

        if(AuthStatusEnum.NODO.getType().equals(old.getAuthStatus())){
            throw new AppException("待审核数据不允许修改");
        }
        this.saveYwSchoolConfigInfo(vo);
        eduSchoolService.clearCache(old.getSchoolId(),old.getAreaCode());
    }


    @Override
    @OpApi(funcCode = "eduSchoolConfig0018", title = "义务教育学校招生计划提交审核", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduSchoolConfig0018",
            operationName = "义务教育学校招生计划提交审核",
            dataType = "schoolConfigId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void tjAudit(SchoolConfigQueryVO vo) {
        Assert.hasText(vo.getSchoolConfigId(), "学校配置id不能为空");
        String schoolConfigId = vo.getSchoolConfigId();
        EduSchoolConfig config = eduSchoolConfigService.getById(schoolConfigId);
        config.setAuthStatus(AuthStatusEnum.NODO.getType());
        eduSchoolConfigService.updateById(config);
        eduSchoolService.clearCache(config.getSchoolId(),config.getAreaCode());
    }

    @Override
    @OpApi(funcCode = "eduSchoolConfig0019", title = "义务教育学校招生计划管理审核", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduSchoolConfig0019",
            operationName = "义务教育学校招生计划管理审核",
            dataType = "schoolConfigId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void auditYwSchoolConfig(SchoolConfigSaveVO vo) {
        int year = 0;
        year = eduHelper.thisYear();
        if(!ObjectUtils.isEmpty(vo.getYear())&&vo.getYear()!=year){
            throw new AppException("不是当前年度数据不允许处理");
        }
        String authStatus = vo.getAuthStatus();
        String schoolConfigId = vo.getSchoolConfigId();
        Assert.hasText(authStatus, "审核状态不能为空");
        Assert.hasText(schoolConfigId, "学校配置id不能为空");
        EduSchoolConfig config = eduSchoolConfigService.getById(schoolConfigId);
        if (ObjectUtils.isEmpty(config)) {
            throw new AppException("查询不到学校配置信息");
        }
        if(StringUtils.isBlank(config.getAuthStatus())){
            throw new AppException("草稿状态不能审核");
        }
        config.setAuthStatus(authStatus);
        config.setAuthRemarks(vo.getAuthRemarks());
        config.setAuthTime(new Date());
        config.setAuthUserId(SessionUtil.getSessionInfo().getUserId());
        eduSchoolConfigService.updateById(config);
        eduSchoolService.clearCache(config.getSchoolId(),config.getAreaCode());
    }

    @Override
    @OpApi(funcCode = "eduSchoolConfig0020", title = "义务教育学校停止招生", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduSchoolConfig0020",
            operationName = "义务教育学校停止招生",
            dataType = "schoolConfigId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void stopYwSchoolRecruit(SchoolConfigSaveVO vo) {
        int year = 0;
        year = eduHelper.thisYear();
        if (!ObjectUtils.isEmpty(vo.getYear()) && vo.getYear() != year) {
            throw new AppException("不是当前年度数据不允许处理");
        }
        Assert.hasText(vo.getSchoolConfigId(), "学校配置id不能为空");
        String schoolConfigId = vo.getSchoolConfigId();
        EduSchoolConfig config = eduSchoolConfigService.getById(schoolConfigId);
        config.setEndTime(new Date());
        eduSchoolConfigService.updateById(config);
        eduSchoolService.clearCache(config.getSchoolId(),config.getAreaCode());
    }


    @Override
    @OpApi(funcCode = "eduSchoolConfig0021", title = "义务教育招生计划退回到待审核", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduSchoolConfig0021",
            operationName = "义务教育招生计划退回到待审核",
            dataType = "schoolConfigId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void rejectSchoolConfig(SchoolConfigSaveVO vo) {
        int year = 0;
        year = eduHelper.thisYear();

        String authStatus = vo.getAuthStatus();
        String schoolConfigId = vo.getSchoolConfigId();
        Assert.hasText(schoolConfigId, "学校配置id不能为空");
        EduSchoolConfig config = eduSchoolConfigService.getById(schoolConfigId);
        if (ObjectUtils.isEmpty(config)) {
            throw new AppException("查询不到学校配置信息");
        }
        if(!ObjectUtils.isEmpty(config.getYear())&&year!=config.getYear()){
            throw new AppException("不是当前年度数据不允许处理");
        }

        //退回到待审核 审核状态置空
        config.setAuthStatus(null);
        config.setAuthRemarks(null);
        config.setAuthTime(null);
        config.setAuthUserId(null);
        eduSchoolConfigService.updateById(config);
        eduSchoolService.clearCache(config.getSchoolId(),config.getAreaCode());
    }


    @Override
    @OpApi(funcCode = "eduSchoolConfig0022", title = "幼儿园恢复招生", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduSchoolConfig0022",
            operationName = "幼儿园恢复招生",
            dataType = "formDetailId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void recoverNurseryRecruit(EduSchoolConfigRecruitVO vo) {
        if (StringUtils.isBlank(vo.getSchoolConfigId())) {
            throw new AppException("招生计划id不能为空");
        }
        EduSchoolConfig eduSchoolConfig = eduSchoolConfigService.getById(vo.getSchoolConfigId());
        if (eduSchoolConfig == null) {
            throw new AppException("招生计划不存在");
        }

        if (vo.getEndTime() == null) {
            EduOperationCacheQueryVO cacheQueryVO = new EduOperationCacheQueryVO();
            cacheQueryVO.setClassify("1");
            cacheQueryVO.setOperating("YRY001");
            cacheQueryVO.setAreaCode(eduSchoolConfig.getAreaCode());
            EduOperationCacheDetailVO eduOperationCacheVO = eduSystemConfigIntegration.getEduOperationOne(cacheQueryVO);
            if (!ObjectUtils.isEmpty(eduOperationCacheVO)) {
                eduSchoolConfig.setEndTime(eduOperationCacheVO.getEndTime());
            }
        } else {
            eduSchoolConfig.setEndTime(vo.getEndTime());
        }
        eduSchoolConfigService.updateById(eduSchoolConfig);
        eduSchoolService.clearCache(eduSchoolConfig.getSchoolId(), eduSchoolConfig.getAreaCode());
    }

}
